package com.yootoo.bsuser.entity;

import com.alibaba.druid.support.json.JSONUtils;

public class MaoPaoRank {

	
	//冒泡排序
	private static void maoPaoRank() {
		long startTime =System.currentTimeMillis();
		int[] numRankArr =new int[10000];
		int oneHundredThousand =10000;
		for (int i=0 ;i<oneHundredThousand; i++) {
			int max=oneHundredThousand,min=1;
            int ran2 = (int) (Math.random()*(max-min)+min); 
            numRankArr[i] =ran2;
		}
		//冒泡排序
		int temp;
		for (int i =0;i<numRankArr.length-1;i++) {
			for (int j =0;j<numRankArr.length-1-i;j++) {
				//System.err.println(j+":"+numRankArr[j] +"比较"+(j+1)+":"+numRankArr[j+1]);
				if(numRankArr[j]>numRankArr[j+1]) {
					temp=numRankArr[j];
					numRankArr[j] =numRankArr[j+1];
					numRankArr[j+1]=temp;
				}
			}
		}
		System.err.println(System.currentTimeMillis()-startTime);
		System.err.println(JSONUtils.toJSONString(numRankArr));
	}
	//选择排序
	private static void choiceRank() {
		long startTime =System.currentTimeMillis();
		int[] numRankArr =new int[10000];
		int oneHundredThousand =10000;
		for (int i=0 ;i<oneHundredThousand; i++) {
			int max=oneHundredThousand,min=1;
            int ran2 = (int) (Math.random()*(max-min)+min); 
            numRankArr[i] =ran2;
		}
		//int[] numRankArr = new int[]{10,8,2,5,1,7,6}; 
		//升序
		int minIndex;
		int temp;
		for (int i =0;i<=numRankArr.length-1;i++) {
			minIndex =i;
			//System.err.println(i+":"+numRankArr[minIndex] +"比较"+(j)+":"+numRankArr[j]+"| index="+minIndex);
			for (int j =0+i;j<=numRankArr.length-1;j++) {
				if(numRankArr[minIndex]>numRankArr[j]) {
					minIndex =j;
				}
			}
			temp =numRankArr[i];
			numRankArr[i] =numRankArr[minIndex];
			numRankArr[minIndex]=temp;
		}
		System.err.println(System.currentTimeMillis()-startTime);
		System.err.println(JSONUtils.toJSONString(numRankArr));
	}
	
	    //插入排序
		private static void insertRank() {
			long startTime =System.currentTimeMillis();
			//int[] numRankArr =new int[10000];
//			int oneHundredThousand =10000;
//			for (int i=0 ;i<oneHundredThousand; i++) {
//				int max=oneHundredThousand,min=1;
//	            int ran2 = (int) (Math.random()*(max-min)+min); 
//	            numRankArr[i] =ran2;
//			}
			int[] numRankArr = new int[]{10,8,2,5,1,7,6}; 
			//升序
			int minIndex;
			int temp;
			for (int i =1;i<=numRankArr.length-1;i++) {
				minIndex =i;
				for (int j =i-1;j>=0;j--) {
					System.err.println(minIndex+":"+numRankArr[minIndex] +"比较"+(j)+":"+numRankArr[j]+"| index="+minIndex);
					if(numRankArr[minIndex]<numRankArr[j]) {
						minIndex =j;
					}
				}
				System.err.println(i+numRankArr[i]+"|"+numRankArr[minIndex]);
				temp =numRankArr[i];
				numRankArr[i] =numRankArr[minIndex];
				numRankArr[minIndex]=temp;
			}
			System.err.println(System.currentTimeMillis()-startTime);
			System.err.println(JSONUtils.toJSONString(numRankArr));
		}
	
	
	
	public static void main(String[] args) {
		//maoPaoRank();
		//choiceRank();
		insertRank();
	}
	
}
